package com.lagou.service.util;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class ZookeeperUtil {


    /**
     * 创建zookeeper会话
     */
    public static CuratorFramework createSession(String ipPortString) {
        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);
        // 使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(ipPortString)
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                // 独立的命名空间 /base
                .namespace("base")
                .build();

        client.start();
        System.out.println("会话创建了");
        return client;
    }
}
